﻿
@COFE ?= {}

$ = jQuery
Entry = COFE.Entry
Link = COFE.Link
EntryItem = COFE.EntryItem
EntryList = COFE.EntryList
EntryItemListBase = COFE.EntryItemListBase
utils = COFE.utils

# CONTROLLER - Represents the entry list view, 
#              it response to App.pathChanged event to change the current path.
#     params - @base.el, @base.App (for listening pathChanged event)
#     events - updated (by base class), rendered
class EntryItemList extends EntryItemListBase
  constructor : () ->
    super                                     # These are overridable:
    @childElementKey = "<li>"                 # 1. child <li> element.
    @childTemplateKey = "#entryItemTemplate"  # 2. css style of child element.

  elements : 
    "#entryList" : "listbox"                  # ul element with data-role="listview" 
    ".header .headerCaption" : "header"       # header text

  # Calls when App.pathChanged event raised.
  onPathChanged : (sender, path) =>    
    super(sender, path)                       # base method update the entry list.    
    @header.text(path.header)  # update the header text.  

  # Calls by base.onPathChanged(), this method then goes back to base.changePath().
  changePath : (path) =>
    # $.mobile.showPageLoadingMsg()
    super

  init : ->    
    @el?.listview()
    @App.bind("pathChanged", @onPathChanged)  # bind the @pathChanged event. 
    @bind("updated", @render)                 # when base.updated event raised, calls render()
    super

  # Render the entryItems to the tag
  render : =>
    @remove()
    for entryItem in @entryItems              # for each entry items (controllers)
      @listbox.append(entryItem.render())     # calls it's render method, and append.
    @listbox.listview('refresh')
    #$.mobile.changePage( $("#list"), { allowSamePageTransition: true} );
    $.mobile.hidePageLoadingMsg()
    @trigger("rendered", @)

  # Clear the rendered items.
  remove: =>    
    @listbox.empty()  
 
  # Overrided, when item clicked
  itemClicked : (entryItem, entry) =>
    super
    if entry.basetype == "folder"
      @App.changePath(Link.fromPath(entry.path))  # folder - change to specific path
    else @App.changeSelection(entry)              # file - change selection to that entry.

COFE.EntryItemList = EntryItemList
